﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using IHISCETeim.BLL;

using NPOI.HSSF.UserModel;
using NPOI.HPSF;
using NPOI.POIFS.FileSystem;
using NPOI.SS.UserModel;
using NPOI.SS.Util;
using NPOI.HSSF.Util;
using IHISCETeim.Library;

namespace IHISCETeim.Web.Export.AgentSys
{
    public partial class ReportMTTR : ExportPageBase
    {
        protected override string xlsTemplateFileName
        {
            get
            {
                return "/Agent/平均维修时间.xls";
            }
        }
        protected override string SheetName
        {
            get
            {
                return "sheet1";
            }

        }

        /// <summary>
        /// 查找条件
        /// </summary>
        protected string sqlwhere
        {
            get
            {
                return GetRequestValue<string>("q", "");
            }
        }
        protected override string SQLSort
        {
            get
            {
                return " QltyId ASC ";
            }
        }

        private DataTableCollection _dts = null;
        protected DataTableCollection dts
        {
            get
            {
                if (_dts == null)
                {
                    int TotalCount = 0;
                    _dts = AgentQltyController.MTTRGetPagination(sqlwhere, 1, 999999, SQLSort, ref TotalCount);
                }
                return _dts;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            InitializeWorkbook();
            ISheet sheet1 = hssfworkbook.GetSheet(SheetName);
            //
            DataTable dtList = dts[0];

            int rowindexBase = 1;
            int rowindex = 0;
            int rowReal = rowindex + rowindexBase;


            foreach (DataRow listDTDR in dtList.Rows)
            {
                int cellReal = 0;
                HSSFRow writerow = (HSSFRow)sheet1.CreateRow(rowReal);
                writerow.CreateCell(cellReal++).SetCellValue(listDTDR["ClaimState"].ToString());
                writerow.CreateCell(cellReal++).SetCellValue(listDTDR["ClaimsNumber"].ToString());
                writerow.CreateCell(cellReal++).SetCellValue(listDTDR["AgentName"].ToString());
                writerow.CreateCell(cellReal++).SetCellValue(listDTDR["SerialNumber"].ToString());
                writerow.CreateCell(cellReal++).SetCellValue(listDTDR["WorkHour"].ToString());
                //writerow.CreateCell(cellReal++).SetCellValue(GetToDateTime(listDTDR["IntoDate"]));
                //writerow.CreateCell(cellReal++).SetCellValue(GetToDateTime(listDTDR["FaultDate"]));
                //writerow.CreateCell(cellReal++).SetCellValue(GetToDateTime(listDTDR["IssueDate"]));

                writerow.CreateCell(cellReal++).SetCellValue((listDTDR["IntoDate"].ToString()));
                writerow.CreateCell(cellReal++).SetCellValue((listDTDR["FaultDate"].ToString()));
                writerow.CreateCell(cellReal++).SetCellValue((listDTDR["IssueDate"].ToString()));

                writerow.CreateCell(cellReal++).SetCellValue(listDTDR["SrvActReplyDate"].ToString());
                writerow.CreateCell(cellReal++).SetCellValue(listDTDR["SrvMangerReplyDate"].ToString());
                writerow.CreateCell(cellReal++).SetCellValue(listDTDR["NoticeSendPartsDate"].ToString());
                writerow.CreateCell(cellReal++).SetCellValue(listDTDR["PartsRequestType"].ToString());
                writerow.CreateCell(cellReal++).SetCellValue(listDTDR["SupperSendDate"].ToString());
                writerow.CreateCell(cellReal++).SetCellValue(listDTDR["ERPSendDate"].ToString());
                writerow.CreateCell(cellReal++).SetCellValue(listDTDR["RealSendDate"].ToString());
                writerow.CreateCell(cellReal++).SetCellValue(listDTDR["AgentAcceptDate"].ToString());
                writerow.CreateCell(cellReal++).SetCellValue(listDTDR["AgentRecoveryDate"].ToString());
                writerow.CreateCell(cellReal++).SetCellValue(listDTDR["品情报告"].ToString());
                writerow.CreateCell(cellReal++).SetCellValue(listDTDR["品情回答"].ToString());
                writerow.CreateCell(cellReal++).SetCellValue(listDTDR["配件准备"].ToString());
                //writerow.CreateCell(cellReal++).SetCellValue(listDTDR["配件发货"].ToString());
                writerow.CreateCell(cellReal++).SetCellValue(listDTDR["物流时间"].ToString());
                writerow.CreateCell(cellReal++).SetCellValue(listDTDR["处理时间"].ToString());
                writerow.CreateCell(cellReal++).SetCellValue(listDTDR["MTTRHR"].ToString());
                writerow.CreateCell(cellReal++).SetCellValue(listDTDR["FaultDescription"].ToString());
                rowReal++;
            }

            //writerowSum.CreateCell(2).SetCellFormula(string.Format("sum(C3:C{0})", rowReal));          

            //
            WriteToFile();
        }

        private string GetToDateTime(object x)
        {
            try {
                return Convert.ToDateTime(x).ToString("yyyy-MM-dd hh:mm");
            }
            catch(Exception EX)
            {
                return "";
            }
        }

        protected override string GetXlsFileName(string oldFileName)
        {
            //return base.GetXlsFileName(oldFileName);

            return string.Format("{0}{1}.xls",DateTime.Now.ToString("yyyy-MM-dd"), "平均维修时间明细表").Trim();
        }
    }
}